#ifndef PROTOCOL_DEFINE_H
#define PROTOCOL_DEFINE_H
#include <QByteArray>
#include <iostream>
#include <qdatastream.h>
#include <QDebug>
#define  OtherSize              20
#define  SendFileBufferSize     1024*4

struct NewHeader{
    char  checkCode1;//校验码1  （121）
    char  checkCode2;//校验码2  （212）
    char  cmdId;//指令号
    char	  reserve;//预留  包对齐
    int	  orderId;//指令次数
    int	  packSize;//数据包包大小
};


struct NewTcpProtocol{
    NewHeader              header;
    //-----------普通消息-------------------------
    char    loginName[32];//登录指令使用
    char    pwd[32];//登录指令使用
    int     loginId;//登录后其他指令  用id判断是否为正常用户
    int     signalType;// ufo ..
    int     signalTypeInfo; // 分类下的详细分类
    int     signalMode;//信号类型
    int     signalRate;//码速
    int     signalRadiation;//辐射
    int     signalLaunchTime;//信号运行时间
    //----------文件传输------------------------
    char	fileName[128];
    int     fileTotalSize;
    int     fileSeek;
    int     fileIsLastPacket;
    int     fileCurrentLen;
    QByteArray fileData;
    QByteArray convertToByteArray(const NewTcpProtocol& protocol) {
        QByteArray byteArray;
        QDataStream stream(&byteArray, QIODevice::WriteOnly);

        // Write 'NewHeader' part
        stream << protocol.header.checkCode1;
        stream << protocol.header.checkCode2;
        stream << protocol.header.cmdId;
        stream << protocol.header.reserve;
        stream << protocol.header.orderId;
        stream << protocol.header.packSize;

        // Write other fields in the 'NewTcpProtocol' struct
        stream.writeRawData(protocol.loginName, 32);
        stream.writeRawData(protocol.pwd, 32);
        stream << protocol.loginId;
        stream << protocol.signalType;
        stream << protocol.signalTypeInfo;
        stream << protocol.signalMode;
        stream << protocol.signalRate;
        stream << protocol.signalRadiation;
        stream << protocol.signalLaunchTime;

        // Write 'file transfer' fields
        stream.writeRawData(protocol.fileName, 128);
        stream << protocol.fileTotalSize;
        stream << protocol.fileSeek;
        stream << protocol.fileIsLastPacket;
        stream << protocol.fileData.size(); // Use fileData.size() for fileCurrentLen

        // Write 'QByteArray' part
        stream.writeRawData(protocol.fileData.constData(), protocol.fileData.size());

        return byteArray;
    }
    NewTcpProtocol(){}
    NewTcpProtocol(QByteArray& arr){

        QDataStream stream(arr);
        int8_t temp; // Temporary variable to hold the char value.

        // Read the values into the temporary variable and then assign them to the struct members.
        stream >> temp; header.checkCode1 = static_cast<char>(temp);
        stream >> temp; header.checkCode2 = static_cast<char>(temp);
        stream >> temp; header.cmdId = static_cast<char>(temp);
        stream >> temp; header.reserve = static_cast<char>(temp);
        // For int and other types, you can read them directly as usual.
        stream >> header.orderId >> header.packSize;
        // Now, you can use the values in the 'header' struct as needed.
        // For example, to print them:
        qDebug() << "checkCode1: " << header.checkCode1;
        qDebug() << "checkCode2: " << header.checkCode2;
        qDebug() << "cmdId: " << header.cmdId;
        qDebug() << "reserve: " << header.reserve;
        qDebug() << "orderId: " << header.orderId;
        qDebug() << "packSize: " << header.packSize;

        // Read other fields in the packet
        stream.readRawData(loginName, 32);
        stream.readRawData(pwd, 32);
        stream >> loginId;
        stream >> signalType;
        stream >> signalTypeInfo;
        stream >> signalMode;
        stream >> signalRate;
        stream >> signalRadiation;
        stream >> signalLaunchTime;

        // File transfer fields
        stream.readRawData(fileName, 128);
        stream >> fileTotalSize;
        stream >> fileSeek;
        stream >> fileIsLastPacket;
        stream >> fileCurrentLen;
        // Now, 'packet' contains all the data from the QByteArray.
        // You can use the data as needed.
        memcpy(fileData.data(),arr.data()+OtherSize,header.packSize-OtherSize);// 需要修改一下OtherSize  大小为除了文件数据外的结构长度
        fileData.resize(header.packSize-OtherSize);
    }
};

struct Header{
    int            packSize;
};

struct TcpProtocol{
    Header              header;
    int                 cmd=1;                          // 指令 1.普通消息 2.文件 3. 文件接受确认 4.开始传输文件(文件名传输)
    int                 isContinues = -1;               //是否重传
    int                 seek        = -1;               //文件 指针
    int                 isLastPack  = -1;               //是否为最后一个包
    QByteArray          data;                           //数据

    QByteArray getStream(){
        QByteArray arr;
        QDataStream packet(&arr,QIODevice::WriteOnly);
        header.packSize = data.size() + OtherSize;
        arr.resize(header.packSize);
        packet<<header.packSize<<cmd<<isContinues<<seek<<isLastPack;
        memcpy(arr.data()+OtherSize,data.data(),data.size());
        return arr;
    }
    TcpProtocol(){}
    TcpProtocol(QByteArray arr){
        QDataStream packet(arr);
        packet>>header.packSize>>cmd>>isContinues>>seek>>isLastPack;
        char* d = new char[header.packSize-OtherSize];
        memcpy(d,arr.data()+OtherSize,header.packSize-OtherSize);
        data = QByteArray(d);
        data.resize(header.packSize-OtherSize);
        delete [] d;
    }
};

#endif // PROTOCOL_DEFINE_H
